תכנות מונחה עצמים משחקים תשע"ו

Similar documents
תוכנה 1 סמסטר א' תשע"א

CS

Advanced Programming & C++ Language

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

תוכנה 1 * לא בהכרח בסדר הזה

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Review: C++ Basic Concepts. Dr. Yingwu Zhu

IS0020 Program Design and Software Tools Midterm, Fall, 2004

תרגול 3 מערכים ופונקציות

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

תוכנה 1 תרגול 2: מערכים ומבני בקרה

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

מבוא לתכנות ב- JAVA תרגול 7

Object-Oriented Principles and Practice / C++

תוכנה 1 * לא בהכרח בסדר הזה

מבוא לתכנות תוכנית שעור מס. 1 1 דר' דרור טובי, המרכז האוניברסיטאי אריאל בשומרון.

תוכנה 1 * לא בהכרח בסדר הזה

Programming in C++: Assignment Week 3

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

Linked List using a Sentinel

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

Program construction in C++ for Scientific Computing

Operator overloading. Conversions. friend. inline

Advanced C++ Topics. Alexander Warg, 2017

תכנות מתקדם בשפת C משתנים

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

CIS 190: C/C++ Programming. Classes in C++

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Working with Batches of Data

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה

ASP.Net Web API.

CPSC 427: Object-Oriented Programming

CS 247: Software Engineering Principles. ADT Design

1 Short Answer (7 Points Each)

COMP6771 Advanced C++ Programming

1 Short Answer (5 Points Each)

Object-Oriented Principles and Practice / C++

a data type is Types

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Data Structures and Algorithms

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

Programming in C++: Assignment Week 3

The paramaterless ctor (aka default ctor)

std::optional from Scratch

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

GEA 2017, Week 4. February 21, 2017

C++ Final Exam 2017/2018

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CS 7B - Spring Final Exam

Engineering Programming A

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

תרגול מספר 3: מערכים

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

היצביט ומ - ןוכית ת וי נבת

Rvalue References & Move Semantics

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

G52CPP C++ Programming Lecture 13

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

การทดลองท 8_2 Editor Buffer Array Implementation

קורס תכנות כתובות בזיכרון כתובות בזכרון מצביעים וכתובות מצביעים וכתובות שיעור שביעי: מבנים, הקצאת זיכרון דינאמית האופרטור &

מבוא למדעי המחשב עבודה # 3

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Fast Introduction to Object Oriented Programming and C++

תור שימושים בעולם התוכנה

Array Elements as Function Parameters

Dynamic Data Structures

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

Overview of C Feabhas Ltd 2012

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

TDDD38 - Advanced programming in C++

C++ Arrays. C++ Spring 2000 Arrays 1

Rvalue References, Move Semantics, Universal References

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

C++ Constructor Insanity

Programming in C++: Assignment Week 4

Class Analysis & Design

CMSC 202 Midterm Exam 1 Fall 2015

University of Toronto

Composition I. composition is a way to combine or compose multiple classes together to create new class

<exp> ::= <define> <cexp> <define> ::= ( define <var-decl> <cexp> ) / DefExp(var:VarDecl, val:cexp)

Consider the program...

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

Arrays in C++ Instructor: Andy Abreu

Creating Templates and Implementing Smart Pointers CS193D 2/27/06

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Due Date: See Blackboard

Computer Programming A תרגול 9

COMP6771 Advanced C++ Programming

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Constructors.

ComS 228 Exam 1. September 27, 2004

Transcription:

move semantics 1 תכנות מונחה עצמים ופיתוח משחקים תשע"ו סמנטיקת ההעברה semantics( )Move

move semantics 2 מטרה האצה של התוכניות, שיפור בביצועים על ידי חסכון בבנייה והעתקה של אובייקטים זמניים

move semantics 3 מהם?rvalue, lvalue lvalue משתנה שניתן לגשת אליו בזיכרון )ניתן להפעיל עליו אופרטור &(. rvalue משתנה שאינו.lvalue מתייחס למשתנה זמני, ללא שם, המיועד להערכה זמנית של ביטוי. דוגמאות: int x = 10; // x is an lvalue and 10 is an rvalue int y = 20; // same x = y; // x and y are lvalues (lvalues can be at any side) int* p = &x; // p is an lvalue. Int *q = &10; // error: can t take address of an rvalue x+(y*z); // y*z is an rvalue

move semantics 4 int foo)( { x = foo(); // foo() is an rvalue foo() = 15; // error: can t assign to an rvalue int& goo)( { x = goo(); // fine: assign lvalue into lvalue goo() = 15; // fine: goo() is an lvalue int* p = &goo(); // fine: goo() is an lvalue

move semantics 5 הגדרה מפורשת של משתנה rvalue int& x; // The traditional reference variable (lvalue) int&& x; // C++11 rvalue reference variable

move semantics 6 שכפול לעומת העברה העברת משאבים במקום שכפול של משאבים משפרת את הביצועים. יש מקומות בהם יהיה מיותר לשכפל ודי בהעברת המשאבים. מתאים במיוחד עבור משתנה rvalue שההתייחסות אליהם בלאו הכי זמנית. דוגמא לשכפול: string func() { string s; //do something with s return s; string mystr=func(); // here is a clone of an rvalue

move semantics 7 Copy constructor and assignment class MemoryPage { public: MemoryPage(int sz=512); ~MemoryPage(); //typical C++03 copy ctor and assignment operator (clone resource) private: ; MemoryPage(const MemoryPage&); MemoryPage& operator=(const MemoryPage&); int m_size; char * m_buf;

move semantics 8 Copy constructor and assignment //typical C++03 copy ctor and assignment operator (clone resource) MemoryPage::MemoryPage(const MemoryPage& other) : m_size(other.m_size), m_buf(new char [m_size]) { for (int i=0;i<_size;++i) m_buf[i] = other.m_buf[i]; MemoryPage& MemoryPage::operator=(const MemoryPage& other) { if (this!=&other) { delete[] m_buf; m_size = other.m_size; m_buf = new char[m_size]; for (int i=0;i<m_size;++i) m_buf[i] = other.m_buf[i]; return *this;

move semantics 9 Move constructor and assignment class MemoryPage { public: MemoryPage(int sz=512); ~MemoryPage(); //typical C++03 copy ctor and assignment operator (clone resource) MemoryPage(const MemoryPage&); MemoryPage& operator=(const MemoryPage&); //C++ 11 move constructor and assignment MemoryPage(MemoryPage&&); MemoryPage& operator=(memorypage&&); private: int m_size; char * m_buf; ;

move semantics 10 Move constructor and assignment MemoryPage::MemoryPage(MemoryPage&& other) : m_size(other.m_size), m_buf(other.m_buf) { other.m_size = 0; other.m_buf = nullptr; MemoryPage& MemoryPage::operator=(MemoryPage&& other) { if (this!=&other) { return *this; delete[] m_buf; m_size = other.m_size; m_buf = other.m_buf; other.m_size = 0; other.m_buf = nullptr;

move semantics 11 Test it all // lets add the following functions // member function void MemoryPage::print() { if (m_buf == nullptr) cout << "page is empty" << endl; else cout << "page is not empty" << endl; // non member function MemoryPage read() { cout << "enter page size:" << endl; int size; cin >> size; return MemoryPage(size);

move semantics 12 Test it all int main() { MemoryPage mp1(10); mp1 = read(); mp1 = move(read()); MemoryPage mp2; mp2 = mp1; mp1.print(); mp2 = move(mp1); mp1.print(); // move assignment // explicit move assignment // assignment // mp1 is not empty // explicit move assignment // mp1 is empty

move semantics 13 שילוב עם הספרייה הסטנדרטית הספרייה הסטנדרטית תומכת ברעיונות של סמנטיקת ההעברה פעולות שונות מכילות העמסה )overloading( למשל לפונקציה ארגומנט מסוג ביחס לסמנטיקה זו vector::push_back) ( שתי גרסאות: l-value המתפקד כ-,const T& ארגומנט מסוג המתפקד כ- r-value &&T, )כפי שהכרנו עד כה( )סמנטיקת ההעברה(

move semantics 14 שילוב עם הספרייה הסטנדרטית int main() { vector<memorypage> vm; MemoryPage mp1(1024); //lvalue vm.push_back(mp1); //push_back(const T&) int main() { vector<memorypage> vm; vm.push_back(memorypage(1024)); //push_back(t&&) vm.push_back(memorypage(2048)); //push_back(t&&) MemoryPage mp1(1024); //lvalue vm.push_back(std::move(mp1)); //calls push_back(t&&)

move semantics 15 סיכום כדי לממש סמנטיקת העברה ביחס למחלקה צריך להגדיר: move constructor move assignment operator במקומות בהם ניתן, מתבצעת העברה של אובייקטים במקום העתקה סמנטיקת ההעברה מאפשרת חיסכון ניכר בהעתקות של אובייקטים ולכן מתקבלים ביצועים טובים באופן משמעותי סמנטיקת ההעברה לא מצריכה שינויים בקוד הקיים השקעה קטנה בקידוד המחלקות שמניבה יתרון משמעותי